﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;
using com.lover.astd.common.manager;
using com.lover.astd.common.model;

namespace com.lover.astd.common.logic
{
    public class TroopMgr : MgrBase
    {
        public TroopMgr() { _logColor = System.Drawing.Color.DarkSalmon; }
        private long _freeArmyCd = 0;
        /// <summary>
        /// 0=成功, 1=征兵失败
        /// </summary>
        /// <returns></returns>
        public int makeSureForce(ProtocolMgr protocol, ILogger logger, double min_troop_percent=0.8)
        {
            User u = protocol.getUser();
            //义兵每次是5%, 那么如果兵百分比小于90%, 则征义兵
            if (u.Forces * 1.0 / u.MaxForces < 0.9)
            {
                if (_freeArmyCd == 0 || (_freeArmyCd > 0 && TimeMgr.TimeStamp > _freeArmyCd))
                {
                    // =0表示当前还没有cd或者未知cd, >0表示已有cd
                    int freeArmyCount = 0;
                    int freearmy_result = getFreeArmy(protocol, logger, out freeArmyCount);
                    if (freearmy_result == 0)
                    {
                        if (freeArmyCount == 0) _freeArmyCd = TimeMgr.TimeStamp + next_day();// 没有义兵了, 等下一天吧
                        else _freeArmyCd = TimeMgr.TimeStamp + 30 * 60 * 1000; // 义兵cd 30分钟
                    }
                    else if (freearmy_result == 1) { }// http error, ignore
                    else if (freearmy_result == 2) { _freeArmyCd = TimeMgr.TimeStamp + 30 * 60 * 1000; } // cd, 直接过30分钟再说
                    else if (freearmy_result == 3) { _freeArmyCd = TimeMgr.TimeStamp + next_day(); } // no free now
                }
            }
            // 首先看部队是否需要征兵
            int mintroop = (int)(u.MaxForces * min_troop_percent);
            if (u.Forces < mintroop)
            {
                // 征兵
                if (!draught(protocol, logger, mintroop - u.Forces)) return 1;
            }
            return 0;
        }
        /// <summary>
        /// 义兵, 每次加最大兵数量的5%, 0=success, 1=http error, 2=cd, 3=no free now
        /// </summary>
        /// <param name="force_number"></param>
        /// <returns></returns>
        private int getFreeArmy(ProtocolMgr protocol, ILogger logger, out int freeArmyCount)
        {
            freeArmyCount = 0;
            string url = "/root/mainCity!rightArmy.action";
            com.lover.astd.common.model.ServerResult result = protocol.getXml(url, "征义兵");
            if (result == null) return 1;
            else if (!result.CmdSucceed)
            {
                if (result.CmdError.Contains("征义兵CD中") || result.CmdError.Contains("征義兵CD中")) return 2;
                else if (result.CmdError.Contains("已用完") || result.CmdError.Contains("已用完")) return 3;
            }
            XmlDocument xml = result.CmdResult;
            //<results><state>1</state><cde>1800000</cde><rightcd>1800000</rightcd><rightnum>8</rightnum><forces>26750</forces></results>
            XmlNode num_node = xml.SelectSingleNode("/results/rightnum");
            if (num_node != null) Int32.TryParse(num_node.InnerText, out freeArmyCount);
            else freeArmyCount = 0;

            int force = 0;
            XmlNode force_node = xml.SelectSingleNode("/results/forces");
            if (force_node != null) Int32.TryParse(force_node.InnerText, out force);

            logInfo(logger, String.Format("自动征义兵, 兵力+{0}, 义兵次数还剩{1}次", force, freeArmyCount));

            return 0;
        }
        /// <summary>
        /// 征兵
        /// </summary>
        /// <param name="force_number"></param>
        /// <returns></returns>
        protected bool draught(ProtocolMgr protocol, ILogger logger, int force_number)
        {
            string url = "/root/mainCity!draught.action";
            string data = "forceNum=" + force_number;
            com.lover.astd.common.model.ServerResult result = protocol.postXml(url, data, "征兵");
            if (result == null || !result.CmdSucceed) return false;
            XmlDocument xml = result.CmdResult;
            return true;
        }
    }
}
